<?
 ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.plugins_path.'/catalog/models/build/classes');

 // load ORM
 require_once 'propel/Propel.php';
 // load infrastructure model
 require_once plugins_path."/catalog/models/infrastructure.php5";


    class Controller_Catalog extends Controller_Base_Admin {
	
	private $infrastructure = null;
    
    function __construct() {
	    parent::__construct();
	    
	    Catalog::init();
		$this->config = Catalog::config();//Config::load(plugins_path.'/catalog/catalog.yaml');
		$this->infrastructure = Catalog::infrastructure();//new Model_Infrastructure($this->config[infrastructure]); 
	}

	function index($args) {

		$c = new Criteria();

		if($this->config[country][name]){
			$c->add(CountriesPeer::COUNTRYNAME, $this->config[country][name]);
		}

		$countries = CountriesPeer::doSelect($c);	
		
		if ( $_GET[county_id] || count($countries) == 1){
			$city_c = new Criteria();
			
			$id = $_GET[county_id] ? $_GET[county_id] : $countries[0]->getCountryId();
			
			$city_c->add(CitiesPeer::COUNTRYID, intval($id));
			
			$cities = CitiesPeer::doSelect($city_c);
		}

		$items_c = new Criteria();
		
		if($_GET[city_id]) {
			$items_c->add(ObjectsPeer::CITYID, intval($_GET[city_id]));
		} else if($_GET[country_id]) {
			$items_c->add(ObjectsPeer::COUNTRYID, $_GET[country_id]);
//			$items_c->setLimit(2);
		}

		if($_GET[type]) {
			$items_c->add(ObjectsPeer::TYPEID, intval($_GET[type]));
		}

		$rows = ObjectsPeer::doSelectJoinCities($items_c);

		$this->template->setArgs(array(
			tabs       	=> $this->config[tabs][main],
			plugin_path	=> plugins_path.'/catalog/tmpl',
			cities		=> $cities,
			countries	=> $countries,
			object_types=> $this->config[object_types],
			
			rows		=> $rows,
			country_id	=> intval($_GET[county_id] ? $_GET[county_id] : $countries[0]->getCountryId()),
			city_id		=> intval($_GET[city_id]),
		));
		
		return $this->template;
	}

	function catalog_add_city($args) {

		if($_POST[name] && $_POST[country] && $_POST[link] && $_POST[photo_url]) {
			$city = new Cities();
			$city->setCityName($_POST[name]);
			$city->setCountryId($_POST[country]);
			$city->setCityLink($_POST[link]);
			$city->setPhotoUrl($_POST[photo_url]);
			
			$city->save();

			return new Redirect('/admin/catalog#catalog-cities');
		} 

		$c = new Criteria();

		if($this->config[country][name]){
			$c->add(CountriesPeer::COUNTRYNAME, $this->config[country][name]);
		}

		$countries = CountriesPeer::doSelect($c);	
		
		$this->template->setArgs(array(
			tabs       	=> $this->config[tabs][city_add],
			plugin_path	=> plugins_path.'/catalog/tmpl',
			countries	=> $countries,			
		));
		
		return $this->template; 
	}

	function catalog_delete_city($args) {
		
		$id = intval($_GET[id]);
		
		if ($id) {
			$item = CitiesPeer::retrieveByPK($id); 
			$item->delete();
		} 
		
		return new Redirect('/admin/catalog#catalog-cities');
	}


	function catalog_delete($args) {
		$id = intval($_GET[id]);
		
		if ($id) {
			$item = ObjectsPeer::retrieveByPK($id); 
			$item->delete();
		} 
		return new Redirect('/admin/catalog');
	}

	function catalog_edit($args) {

		$id = intval($_GET[id]);
		
		if ($id) {
			$item = ObjectsPeer::retrieveByPK($id); 
		} else {
			return new Redirect('/admin/catalog'); 
		} 

		if($_POST)
			$this->doSave($item);


		$c = new Criteria();
		$c->add(CountriesPeer::COUNTRYNAME, 'Турция');
		$countries = CountriesPeer::doSelect($c);	

		$cc = new Criteria();
		if(count($countries) == 1 ){
			$cc->add(CitiesPeer::COUNTRYID, $countries[0]->getCountryId());		
		} 

		$cities = CitiesPeer::doSelect($cc);

		$this->template->setArgs(array(
			tabs       			=> $this->config[tabs][edit],
			plugin_path			=> plugins_path.'/catalog/tmpl',
			name				=> $item->getObjectName(),
			photo_url			=> $item->getPhotoUrl(),
			city				=> $item->getCityId(),
			type				=> $item->getTypeId(),
			contry				=> $item->getCountryId(),
			price				=> $item->getPrice(),
			beach				=> $item->getBeach(),
			airport				=> $item->getAirport(),
			city_center			=> $item->getCityCenter(),
			short_description	=> $item->getShortDescription(),
			description			=> $item->getDescription(),	
			item_props 			=> $this->infrastructure->getStringByMask($item->getInfrastructure()),
			props				=> $this->infrastructure->getList(),
			
			object_types		=> $this->config[object_types],
			form_action 		=> "/admin/catalog/catalog_edit?id=".$_GET[id],
			cities		=> $cities,
			countries	=> $countries,
//			rows		=> $rows = ObjectsPeer::doSelect($items_c),
//			country_id	=> intval($_GET[county_id]),
//			city_id		=> intval($_GET[city_id]),
		));
		
		return $this->template;
	}

	function doSave($item = null) {

		if( $_POST[name] && $_POST[city] && $_POST[type]  && $_POST[country]  && $_POST[price] &&  $_POST[beach]  && $_POST[airport]  && $_POST[short_description]) {
			
			if(empty($item))
				$item = new Objects();  
			
			$item->setObjectName($_POST[name]);
			$item->setPhotoUrl($_POST[photo_url]);
			$item->setInfrastructure($this->infrastructure->getMaskByList($_POST['props']));
			$item->setCityId($_POST[city]);
			$item->setTypeId($_POST[type]);
			$item->setCountryId($_POST[country]);
			$item->setPrice($_POST[price]);
			$item->setBeach($_POST[beach]);
			$item->setAirport($_POST[airport]);
			$item->setCityCenter($_POST[city_center]);
			$item->setShortDescription($_POST[short_description]);
			$item->setDescription($_POST[description]);
			$item->save();
			
			if(!$_POST[no_redirect]) {
				return new Redirect('/admin/catalog');
			}
			
		} else if ($_POST) {

			$errors = array('less_arguments');


			$this->template->setArg(
				'errors', 
				$this->template->getArg('errors') 
				? array_merge( $this->template->getArg('errors'), array('less_arguments') )
				: array('less_arguments')
			); 
			
			$this->template->setArgs(array(
				name 				=> $_POST[name],
				photo_url 			=> $_POST[photo_url],
				city				=> $_POST[city],
				type				=> $_POST[type],
				price				=> $_POST[type],
				beach				=> $_POST[beach],
				airport				=> $_POST[airport],
				city_center			=> $_POST[city_center], 
				short_description 	=> $_POST[short_description],
				description			=> $_POST[description],
			));
						
		}
		
	}


	function catalog_add($args) {

		if($_POST)
			$this->doSave();

		$c = new Criteria();
		$c->add(CountriesPeer::COUNTRYNAME, 'Турция');
		$countries = CountriesPeer::doSelect($c);	

		$cc = new Criteria();
		if(count($countries) == 1 ){
			$cc->add(CitiesPeer::COUNTRYID, $countries[0]->getCountryId());		
		} 

		$cities = CitiesPeer::doSelect($cc);

		$this->template->setArgs(array(
			tabs       	=> $this->config[tabs][add],
			plugin_path	=> plugins_path.'/catalog/tmpl',
			countries	=> $countries,
			cities		=> $cities,
			action		=> 'add',
			
			form_action => "/admin/catalog/catalog_add",
			props		=> $this->infrastructure->getList(),
			object_types=> $this->config[object_types],
			no_redirect	=> $this->config[no_redirect] ? 1 : 0,
		));
		
		return $this->template;		
		
	}

/*

	private function init() {
	    $props = new Model_Props();
	    $this->props = $props;
	    $this->props_list = $props->getList();
	}

	function index($admin) {
	    $catalog = new Model_Catalog();

            $category = $_GET['category'] ? $_GET['category'] : 0;

	    $this->template->setArgs(array(
	    	goods      => $catalog->get(array(parent => intval($category))),
		categories => $catalog->getAllCategories(),
		menu       => $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
		tabs       => $this->tabs->get('catalog'),
		allprops   => $this->props_list,
	    ));

	    return $this->template;
	}

	function catalog_add($args) {

	    if($_POST[action] === 'add') {
                $catalog = new Model_Catalog();

		$props = $this->props->getMaskByList($_POST['props']);

                $id = $catalog->add(array($_POST['name'], $_POST['desc'], $props, $_POST['notes'], $_POST['capacity'],'good', $_POST[price1], $_POST[price2], $_POST[price3], $_POST[title]));

		$catalog->saveStructure(array(
		    id     => $id, 
                    parent => $_POST['category']
		));

		return new Redirect("/admin/catalog?category=".$_POST['category']);
            }

	    if ($_POST[action] === 'add_photo' ) {
	    	$catalog = new Model_Catalog();
	    	$catalog->addPhoto($_POST[url],$_POST[id]);
		return new Redirect($_POST[back_uri]);
	    }

            $catalog = new Model_Catalog();

            $this->template->setArgs(array(
                menu       => $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
                tabs       => $this->tabs->get('catalog_add'),
		props      => $this->props_list,
		categories => $catalog->getAllCategories(),
            ));


            return $this->template;
	
	}

	function catalog_edit($args) {
	     $catalog = new Model_Catalog();

	     if($_POST['action'] === 'edit') {
	         $catalog = new Model_Catalog();
		 $props = $this->props->getMaskByList($_POST['props']);
		 $catalog->save(array($_POST['name'], $_POST['desc'], $props, $_POST['notes'], $_POST['capacity'], $_POST[price1], $_POST[price2], $_POST[price3], $_POST[title] , $_POST['id']));

                $catalog->saveStructure(array(
                    id     => $_POST['id'],
                    parent => $_POST['category']
                ));
		 
		 return new Redirect("/admin/catalog?category=".$_POST['category']);
	     }

	     if($_POST[action] === 'set_default_photo') {
	     	$catalog = new Model_Catalog();
		$catalog->setDefaultPhoto($_POST[default_photo],$_POST[id]);

		return new Redirect($_POST[back_uri]);
	     }

	     $item = $catalog->get(array(id => $args[0]));

	     if(!$args[0]) {
	         return new Redirect("/admin/catalog/");
	     }

             $this->template->setArgs(array(
                menu       => $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
                tabs       => $this->tabs->get('catalog_edit'),
                props      => $this->props_list,
		name       => $item->name,
		desc       => $item->desc,
		long_desc  => $item->long_desc,
		price1     => $item->price1,
		price2     => $item->price2,
		price3     => $item->price3,
		title	   => $item->title,
		capacity   => $item->capacity,
		item_props => $this->props->getListByMask($item->props),
		id         => $item->id,
		parent     => $item->parent,
		notes      => $item->notes,
		categories => $catalog->getAllCategories(),
		photos     => $catalog->getPhotos($item->id),
		photo      => $item->photo,
            ));

            return $this->template;
	}

	function catalog_delete($args) {
            $catalog = new Model_Catalog();
            
	    $item = $catalog->get(array(id => $args[0]));

	    if($args[0]) {
	        $catalog->delete($args[0]);
	    }
            

	    return new Redirect('/admin/catalog?category='.$item->parent);
	}
*/
}